package com.dushibao.admin.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.dushibao.admin.model.Admin;
import com.dushibao.utils.IpUtils;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.util.Date;

@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();

//        String simepleName = metaObject.getOriginalObject().getClass().getSimpleName();
//        System.out.println(simepleName);

        this.fillStrategy(metaObject, "addTime", new Date());

//        if(Admin.class.getSimpleName().equals(simepleName)){
        if(metaObject.getOriginalObject() instanceof Admin){
            this.fillStrategy(metaObject, "loginDate", new Date());
            this.fillStrategy(metaObject, "loginIp", IpUtils.getIPAddress(requestAttributes.getRequest()));
        }

    }

    @Override
    public void updateFill(MetaObject metaObject) {

    }
}